package com.zjitc.service;

import com.zjitc.dao.RoleDao;
import com.zjitc.dao.UserDao;
import com.zjitc.dao.UserRoleDao;
import com.zjitc.pojo.Role;
import com.zjitc.pojo.User;
import com.zjitc.pojo.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IDEA
 * author:lhl
 * Date:2019/2/19 0019
 * Time:12:45
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    RoleDao roleDao;
    @Autowired
    UserRoleDao userRoleDao;
    @Override
    public User login(String username, String password, int state) {
        User user = userDao.findOne(username, state);
        if (user!=null){
            if (user.getPassword().equals(password)){
                return user;
            }else {
                return null;
            }
        }
        return null;
    }

    @Override
    public List<User> getAllGuest() {
        List<User> users = userDao.findAllByState(1);
        if (users!=null&&!users.isEmpty()){
            return users;
        }
        return null;
    }

    @Override
    public List<User> getAllKefu() {
        Role role = roleDao.findOne("客服");
        List<User>userList=new ArrayList<>();
        if (role!=null){
            List<UserRole> roles = userRoleDao.findAll(role.getId());
            for (UserRole userRole:roles){
                User user = userDao.findById(userRole.getUserId());
                userList.add(user);
            }
        }
        return userList;
    }
}
